<template>
  <div>
    <el-card shadow="never">
      <h3>申请列表</h3>
    </el-card>

    <br>

    <data-table :table-data="tableData">
      <el-table-column
        label="申请人"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.nickName }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="申请日期"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.createTime|formatDate2 }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="理由"
        align="center"
        width="100"
        type="expand"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.reason }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="申请结果"
        align="center"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state === 0" type="warning">申请中</el-tag>
          <el-tag v-if="scope.row.state === 1" type="success">申请成功</el-tag>
          <el-tag v-if="scope.row.state === 2" type="danger">申请失败</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="申请处理"
        align="center"
      >
        <template slot-scope="scope">
          <el-button v-if="scope.row.state === 0" size="mini" type="success" @click="updateState(scope.row, 1)">同意 </el-button>
          <el-button v-if="scope.row.state === 0" size="mini" type="danger" @click="updateState(scope.row, 2)">拒绝 </el-button>
        </template>
      </el-table-column>
    </data-table>
  </div>
</template>

<script>

import * as applyApi from '@/api/apply'
import * as userApi from '@/api/user'
import DataTable from '@/components/DataTable'
export default {
  name: '',
  components: {
    DataTable
  },
  data() {
    return {
      addForm: {},
      tableData: []
    }
  },
  created() {
    this.getData()
  },
  methods: {
    getData() {
      applyApi.list().then(res => {
        this.tableData = res.data
        console.log(res.data)
      })
    },
    updateState(row, state) {
      row.state = state
      applyApi.update(row).then(resp => {
        console.log(resp)
        this.$notifySuccess('操作成功')
      })
      if (state === 1) {
        // 更新用户的角色
        const data = {
          id: row.userId,
          roles: '2'
        }
        userApi.updateUser(data)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 50%;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>
